<cfcomponent displayname="ProjectService" extends="actionpacks.projectshen.model.AbstractBean" output="false">

	<cffunction name="setPersistenceService" returntype="void" access="public" output="false" hint="Import: Persistence Service">
		<cfargument name="persistenceService" required="true"/>
		<cfset variables.persistenceService = arguments.persistenceService />
	</cffunction>

	<cffunction name="getPersistenceService" returntype="any" access="private" output="false" hint="Returns Internal: Persistence Service">
		<cfreturn variables.persistenceService />
	</cffunction>

	<cffunction name="setBeanInjector" returntype="void" access="public" output="false" hint="Import: Persistence Service">
		<cfargument name="beanInjector" required="true"/>
		<cfset variables.beanInjector = arguments.beanInjector />
	</cffunction>

	<cffunction name="getBeanInjector" returntype="any" access="private" output="false" hint="Returns Internal: Persistence Service">
		<cfreturn variables.beanInjector />
	</cffunction>

	<cffunction name="setG11nService" returntype="void" access="public" output="false" hint="coldspring:: Project Service">
		<cfargument name="G11nService" required="true"/>
		<cfset variables.G11nService = arguments.G11nService />
	</cffunction>

	<cffunction name="getG11nService" returntype="any" access="private" output="false" hint="internal: Project Service">
		<cfreturn variables.G11nService />
	</cffunction>


	<cffunction name="init" returntype="any" output="false" hint="Constructor">
		<cfargument name="G11nService" required="true"/>
		<cfset variables.conf = arguments.G11nService.getRB("persistence").getResourceBundleData()>		
		<cfreturn this />
	</cffunction>

	<cffunction name="createProjectObject" returntype="any" output="false" hint="Create Project Object">
		<cfargument name="componentName" default="actionpacks.projectshen.model.project.Project">
		<cfset var projectObject = createObject("component",arguments.componentName)>
		<cfreturn projectObject />
	</cffunction>

	<cffunction name="runObjectQuery" access="private" returntype="any" output="false" roles="" hint="Returns: query">
		<cfargument name="objectname" default="" />
		<cfargument name="params" default="#structNew()#" />
		<cfset var conf = variables.conf />
		<cfset var ormObjectName = conf[arguments.objectname] />
		<cfset var query = getPersistenceService().query(ormObjectName,params)>
	  <cfreturn query />	
	</cffunction>

	<cffunction name="getObjectByID" access="private" returntype="any" output="false" roles="" hint="Returns: query">
		<cfargument name="objectname" required="true"  />
		<cfargument name="primarykey" required="true"  />
		<cfargument name="id" required="true" />
		<cfset var priKeys = structNew() />
		<cfset var conf = variables.conf />
		<cfset var ormObjectKey = conf[arguments.primarykey] />
		<cfset var ormObjectName = conf[arguments.objectname] />
		<cfset var ormObject = "" />
		<cfset priKeys[ormObjectKey] = arguments.id />
		<cfset ormObject = getPersistenceService().load(ormObjectName,priKeys)>
	  <cfreturn ormObject />	
	</cffunction>

	<cffunction name="getObject" access="private" returntype="any" output="false" roles="" hint="Returns: query">
		<cfargument name="objectname" default="" />
		<cfargument name="params" default="#structNew()#" />
		<cfset var conf = variables.conf />
		<cfset var ormObjectName = conf[arguments.objectname] />
		<cfset var ormobject = conf[arguments.objectname] />
		<cfset ormobject = getPersistenceService().load(ormObjectName,arguments.params)>
	  <cfreturn ormobject />	
	</cffunction>

	<cffunction name="setProjectConfigDefaults" access="public" returntype="any" output="false" roles="" hint="">
		<cfargument name="project" type="numeric" required="true">
		<cfset var projectID = arguments.project>
		<cfset var defaultsSQL = "">
		<cfset var sqlQry = "">
		<cfsavecontent variable="defaultsSQL">
			<cfoutput>
			DELETE FROM `projectcomponents` WHERE projectId = '#projectId#';
			DELETE FROM `projectmilestones` WHERE projectId = '#projectId#';
			DELETE FROM `projectpriorities` WHERE projectId = '#projectId#';
			DELETE FROM `projectticketactions` WHERE projectId = '#projectId#';
			DELETE FROM `projectticketresolutions` WHERE projectId = '#projectId#';
			DELETE FROM `projectticketseverities` WHERE projectId = '#projectId#';
			DELETE FROM `projectticketstatuses` WHERE projectId = '#projectId#';
			DELETE FROM `projecttickettypes` WHERE projectId = '#projectId#';
			DELETE FROM `projectversions` WHERE projectId = '#projectId#';

			INSERT INTO `projectcomponents` (`componentId`, `projectId`, `componentName`, `componentDescription`, `createUserID`) VALUES ('0', '#projectId#', 'Component 1', 'Component 1', '3');
			INSERT INTO `projectcomponents` (`componentId`, `projectId`, `componentName`, `componentDescription`, `createUserID`) VALUES ('0', '#projectId#', 'Component 2', 'Component 2', '5');
			INSERT INTO `projectmilestones` (`milestoneId`, `projectId`, `milestoneName`, `milestoneDescription`, `milestoneDueDate`, `milestoneCompleted`, `createUserId`, `createDate`, `modifiedDate`)	VALUES ('0', '#projectId#', 'milestone one', 'some desc for milestone', null, '0', 1, now(), now());
			INSERT INTO `projectmilestones` (`milestoneId`, `projectId`, `milestoneName`, `milestoneDescription`, `milestoneDueDate`, `milestoneCompleted`, `createUserId`, `createDate`, `modifiedDate`) VALUES ('0', '#projectId#', 'milestone two', 'more descriptive milestone stuff', null, '0', 1, now(), now());
			INSERT INTO `projectpriorities` (`priorityId`, `projectId`, `priorityName`, `priorityDescription`, `createUserId`, `createDate`, `modifiedUserId`, `modifiedDate`) VALUES ('0', '#projectId#', 'highest', null, 1, now(), 1, now());
			INSERT INTO `projectpriorities` (`priorityId`, `projectId`, `priorityName`, `priorityDescription`, `createUserId`, `createDate`, `modifiedUserId`, `modifiedDate`) VALUES ('0', '#projectId#', 'high', null, 1, now(), 1, now());
			INSERT INTO `projectpriorities` (`priorityId`, `projectId`, `priorityName`, `priorityDescription`, `createUserId`, `createDate`, `modifiedUserId`, `modifiedDate`) VALUES ('0', '#projectId#', 'normal', null, 1, now(), 1, now());
			INSERT INTO `projectpriorities` (`priorityId`, `projectId`, `priorityName`, `priorityDescription`, `createUserId`, `createDate`, `modifiedUserId`, `modifiedDate`) VALUES ('0', '#projectId#', 'low', null, 1, now(), 1, now());
			INSERT INTO `projectpriorities` (`priorityId`, `projectId`, `priorityName`, `priorityDescription`, `createUserId`, `createDate`, `modifiedUserId`, `modifiedDate`) VALUES ('0', '#projectId#', 'lowest', null, 1, now(), 1, now());
			INSERT INTO `projectticketactions` (`actionId`, `projectId`, `actionName`, `actionDescription`, `createUserId`, `createDate`, `modifiedUserId`, `modifiedDate`) VALUES ('0', '#projectId#', 'leave', null, 1, now(), 1, now());
			INSERT INTO `projectticketactions` (`actionId`, `projectId`, `actionName`, `actionDescription`, `createUserId`, `createDate`, `modifiedUserId`, `modifiedDate`) VALUES ('0', '#projectId#', 'resolve', null, 1, now(), 1, now());
			INSERT INTO `projectticketactions` (`actionId`, `projectId`, `actionName`, `actionDescription`, `createUserId`, `createDate`, `modifiedUserId`, `modifiedDate`) VALUES ('0', '#projectId#', 'reassign', null, 1, now(), 1, now());
			INSERT INTO `projectticketactions` (`actionId`, `projectId`, `actionName`, `actionDescription`, `createUserId`, `createDate`, `modifiedUserId`, `modifiedDate`) VALUES ('0', '#projectId#', 'accept', null, 1, now(), 1, now());
			INSERT INTO `projectticketactions` (`actionId`, `projectId`, `actionName`, `actionDescription`, `createUserId`, `createDate`, `modifiedUserId`, `modifiedDate`) VALUES ('0', '#projectId#', 'reopen', null, 1, now(), 1, now());
			INSERT INTO `projectticketresolutions` (`resolutionId`, `projectId`, `resolutionName`, `resolutionDescription`, `createUserId`, `createDate`, `modifiedUserId`, `modifiedDate`) VALUES ('0', '#projectId#', 'fixed', null, 1, now(), 1, now());
			INSERT INTO `projectticketresolutions` (`resolutionId`, `projectId`, `resolutionName`, `resolutionDescription`, `createUserId`, `createDate`, `modifiedUserId`, `modifiedDate`) VALUES ('0', '#projectId#', 'invalid', null, 1, now(), 1, now());
			INSERT INTO `projectticketresolutions` (`resolutionId`, `projectId`, `resolutionName`, `resolutionDescription`, `createUserId`, `createDate`, `modifiedUserId`, `modifiedDate`) VALUES ('0', '#projectId#', 'wontfix', null, 1, now(), 1, now());
			INSERT INTO `projectticketresolutions` (`resolutionId`, `projectId`, `resolutionName`, `resolutionDescription`, `createUserId`, `createDate`, `modifiedUserId`, `modifiedDate`) VALUES ('0', '#projectId#', 'duplicate', null, 1, now(), 1, now());
			INSERT INTO `projectticketresolutions` (`resolutionId`, `projectId`, `resolutionName`, `resolutionDescription`, `createUserId`, `createDate`, `modifiedUserId`, `modifiedDate`) VALUES ('0', '#projectId#', 'worksforme', null, 1, now(), 1, now());
			INSERT INTO `projectticketseverities` (`severityId`, `projectId`, `severityName`, `severityDescription`, `createUserId`, `createDate`, `modifiedUserId`, `modifiedDate`) VALUES ('0', '#projectId#', 'blocker', null, 1, now(), 1, now());
			INSERT INTO `projectticketseverities` (`severityId`, `projectId`, `severityName`, `severityDescription`, `createUserId`, `createDate`, `modifiedUserId`, `modifiedDate`) VALUES ('0', '#projectId#', 'critical', null, 1, now(), 1, now());
			INSERT INTO `projectticketseverities` (`severityId`, `projectId`, `severityName`, `severityDescription`, `createUserId`, `createDate`, `modifiedUserId`, `modifiedDate`) VALUES ('0', '#projectId#', 'major', null, 1, now(), 1, now());
			INSERT INTO `projectticketseverities` (`severityId`, `projectId`, `severityName`, `severityDescription`, `createUserId`, `createDate`, `modifiedUserId`, `modifiedDate`) VALUES ('0', '#projectId#', 'normal', null, 1, now(), 1, now());
			INSERT INTO `projectticketseverities` (`severityId`, `projectId`, `severityName`, `severityDescription`, `createUserId`, `createDate`, `modifiedUserId`, `modifiedDate`) VALUES ('0', '#projectId#', 'minor', null, 1, now(), 1, now());
			INSERT INTO `projectticketseverities` (`severityId`, `projectId`, `severityName`, `severityDescription`, `createUserId`, `createDate`, `modifiedUserId`, `modifiedDate`) VALUES ('0', '#projectId#', 'trivial', null, 1, now(), 1, now());
			INSERT INTO `projectticketstatuses` (`statusId`, `projectId`, `statusName`, `statusDescription`, `createUserId`, `createDate`, `modifiedUserId`, `modifiedDate`) VALUES ('0', '#projectId#', 'new', null, 1, now(), 1, now());
			INSERT INTO `projectticketstatuses` (`statusId`, `projectId`, `statusName`, `statusDescription`, `createUserId`, `createDate`, `modifiedUserId`, `modifiedDate`) VALUES ('0', '#projectId#', 'assigned', null, 1, now(), 1, now());
			INSERT INTO `projectticketstatuses` (`statusId`, `projectId`, `statusName`, `statusDescription`, `createUserId`, `createDate`, `modifiedUserId`, `modifiedDate`) VALUES ('0', '#projectId#', 'opened', null, 1, now(), 1, now());
			INSERT INTO `projectticketstatuses` (`statusId`, `projectId`, `statusName`, `statusDescription`, `createUserId`, `createDate`, `modifiedUserId`, `modifiedDate`) VALUES ('0', '#projectId#', 'reopened', null, 1, now(), 1, now());
			INSERT INTO `projectticketstatuses` (`statusId`, `projectId`, `statusName`, `statusDescription`, `createUserId`, `createDate`, `modifiedUserId`, `modifiedDate`) VALUES ('0', '#projectId#', 'closed', null, 1, now(), 1, now());
			INSERT INTO `projecttickettypes` (`typeId`, `projectId`, `typeName`, `typeDescription`, `createUserId`, `createDate`, `modifiedUserId`, `modifiedDate`) VALUES ('0', '#projectId#', 'defect', null, 1, now(), 1, now());
			INSERT INTO `projecttickettypes` (`typeId`, `projectId`, `typeName`, `typeDescription`, `createUserId`, `createDate`, `modifiedUserId`, `modifiedDate`) VALUES ('0', '#projectId#', 'enhancement', null, 1, now(), 1, now());
			INSERT INTO `projecttickettypes` (`typeId`, `projectId`, `typeName`, `typeDescription`, `createUserId`, `createDate`, `modifiedUserId`, `modifiedDate`) VALUES ('0', '#projectId#', 'task', null, 1, now(), 1, now());
			INSERT INTO `projectversions` (`versionId`, `projectId`, `versionName`, `versionDescription`, `versionTime`, `createUserId`, `createDate`, `modifiedUserId`, `modifiedDate`) VALUES ('0', '#projectId#', '1.0', null, null, 1, now(), 1, now());
			INSERT INTO `projectversions` (`versionId`, `projectId`, `versionName`, `versionDescription`, `versionTime`, `createUserId`, `createDate`, `modifiedUserId`, `modifiedDate`) VALUES ('0', '#projectId#', '2.0', null, null, 1, now(), 1, now());
			</cfoutput>
		</cfsavecontent>
		<cfloop list="#defaultsSQL#" delimiters=";" index="sqlQry">
			<cfquery datasource="projects">
				#preserveSingleQuotes(sqlQry)#
			</cfquery>
		</cfloop>
		
		<cfabort>
	</cffunction>

	<cffunction name="getProjectsQuery" access="public" returntype="any" output="false" roles="" hint="Returns: project">
		<cfargument name="params" default="#structNew()#" />
		<cfset var query = runObjectQuery("project.objectname",arguments.params)>
	  <cfreturn query />
	</cffunction>

	<cffunction name="getProject" access="public" returntype="any" output="false" roles="" hint="Returns: project">
		<cfargument name="params" default="#structNew()#" />
		<cfset var query = runObjectQuery("project.objectname",arguments.params)>
	  <cfreturn query />
	</cffunction>

	<cffunction name="getProjectById" access="public" returntype="any" output="false" roles="" hint="Returns: project">
		<cfargument name="projectId" required="true" />
<!---
 		<cfset getBeanInjector().autowire(projectObject)>
 --->
	  <cfreturn getObjectById("project.objectname","project.primarykey",arguments.projectId)>
	</cffunction>

	<cffunction name="getProjectBySafeName" access="public" returntype="any" output="false" roles="" hint="Returns: project">
		<cfargument name="projectname" required="true" />
		<cfset var projectObject = createProjectObject()>
		<cfset projectObject.setProjectName(rereplacenocase(arguments.projectname,'[^a-z0-9_]','','all'))>
		<cfset projectObject = getPersistenceService().persisted(projectObject)>
	  <cfreturn projectObject>
	</cffunction>
	
	<cffunction name="getProjectComponentsQuery" access="public" returntype="any" output="false" roles="" hint="Returns: project">
		<cfargument name="params" default="#structNew()#" />
		<cfset var query = runObjectQuery("project.component.objectname",arguments.params)>
	  <cfreturn query />
	</cffunction>
	
	<cffunction name="getProjectComponent" access="public" returntype="any" output="false" roles="" hint="Returns: project component">
		<cfargument name="params" default="#structNew()#" />
		<cfset var query = getObject("project.component.objectname",arguments.params)>
	  <cfreturn query />
	</cffunction>
	
	<cffunction name="getProjectMilestonesQuery" access="public" returntype="any" output="false" roles="" hint="Returns: project">
		<cfargument name="params" default="#structNew()#" />
		<cfset var query = runObjectQuery("project.milestone.objectname",arguments.params)>
	  <cfreturn query />
	</cffunction>
	
	<cffunction name="getProjectMilestone" access="public" returntype="any" output="false" roles="" hint="Returns: project">
		<cfargument name="params" default="#structNew()#" />
		<cfset var query = getObject("project.milestone.objectname",arguments.params)>
	  <cfreturn query />
	</cffunction>
	
	<cffunction name="getProjectPrioritiesQuery" access="public" returntype="any" output="false" roles="" hint="Returns: project">
		<cfargument name="params" default="#structNew()#" />
		<cfset var query = runObjectQuery("project.priority.objectname",arguments.params)>
	  <cfreturn query />
	</cffunction>

	<cffunction name="getProjectPriority" access="public" returntype="any" output="false" roles="" hint="Returns: project">
		<cfargument name="params" default="#structNew()#" />
		<cfset var query = getObject("project.priority.objectname",arguments.params)>
	  <cfreturn query />
	</cffunction>
	
	<cffunction name="getProjectVersionsQuery" access="public" returntype="any" output="false" roles="" hint="Returns: project">
		<cfargument name="params" default="#structNew()#" />
		<cfset var query = runObjectQuery("project.version.objectname",arguments.params)>
	  <cfreturn query />
	</cffunction>
	
	<cffunction name="getProjectVersion" access="public" returntype="any" output="false" roles="" hint="Returns: project">
		<cfargument name="params" default="#structNew()#" />
		<cfset var query = getObject("project.version.objectname",arguments.params)>
	  <cfreturn query />
	</cffunction>
	
</cfcomponent>